<template>
  <div>
    <el-row>
      <label class="row-lable">用户id:</label>
      <el-input style="width:160px" v-model="userId"></el-input>
      <label class="row-lable">微信昵称:</label>
      <el-input style="width:160px" v-model="wxNick"></el-input>
      <label class="row-lable">昵称:</label>
      <el-input style="width:160px" v-model="nick"></el-input>
      <label class="row-lable">手机号:</label>
      <el-input style="width:160px" v-model="telephone"></el-input>
      <br>
      <br>
      <label class="row-lable">邀请人id</label>
      <el-input style="width:160px" v-model="agentId"></el-input>

      <label title="用户类型"></label>
      <el-select
        placeholder="请选择用户"
        @change="jump(1)"
        style="width: 150px"
        v-model="userType"
        clearable>
        <el-option v-for="item in types" :key="item.value" :label="item.label" :value="item.value"></el-option>
      </el-select>
      <label title="用户等级"></label>
      <el-select
        placeholder="请选择用户等级"
        @change="jump(1)"
        style="width: 150px"
        v-model="vipLevel"
        clearable>
        <el-option v-for="item in level" :key="item.value" :label="item.label" :value="item.value"></el-option>
      </el-select>

      <el-button type="primary" @click="jump(1)">搜索</el-button>
      <el-button type="primary" @click="openVoucher()">分发抵用券</el-button>
    </el-row>

    <el-table :data="userData" border highlight-current-row stripe style="width: 100%;margin-top: 20px"
              @sort-change="sortChange">
      <el-table-column label="用户id" prop="userId"></el-table-column>
      <el-table-column label="用户昵称" prop="nick"></el-table-column>
      <el-table-column label="手机" prop="telephone"></el-table-column>
      <el-table-column label="微信">
        <template slot-scope="scope">
          <template v-if="scope.row.wxNick">
            <img :src="scope.row.wxHeadUrl" style="width:70px;height: 70px;"/>
            {{scope.row.wxNick}}
          </template>
        </template>
      </el-table-column>
      <el-table-column label="余额">
        <template slot-scope="scope">
          <el-button type="text" @click="open(scope.row.userId)">{{scope.row.mallMoney}}</el-button>
          <el-button
            type="text"
            @click="openReward(scope.row.userId)"
            v-if="scope.row.vipLevel ===4">
            平台奖励
          </el-button>
        </template>
      </el-table-column>
      <el-table-column prop="voucher" label="抵用券余额">
      </el-table-column>
      <el-table-column prop="achievement" label="销售额"></el-table-column>
      <el-table-column prop="inviteNum" label="邀请人数" sortable="custom"></el-table-column>
      <el-table-column prop="inviteVipNum" label="邀请成为vip数" sortable="custom"></el-table-column>
      <el-table-column prop="staffNum" label="店员数量"></el-table-column>
      <el-table-column prop="ownerNum" label="店长数量"></el-table-column>
      <el-table-column prop="bossNum" label="boss数量"></el-table-column>
      <el-table-column prop="shareNum" label="分享成功商品数" sortable="custom"></el-table-column>
      <el-table-column label="用户等级">
        <template slot-scope="scope">
          <template v-if="scope.row.vipLevel ===1">普通顾客</template>
          <template v-if="scope.row.vipLevel ===undefined">普通顾客</template>
          <template v-if="scope.row.vipLevel ===2">店员</template>
          <template v-if="scope.row.vipLevel ===99">管理员</template>
          <el-button type="text" @click="changeLv(scope.row.userId,2)"
                     v-if="scope.row.vipLevel ===3 || scope.row.vipLevel ===4">
            设为店员
          </el-button>
          <br>
          <template v-if="scope.row.vipLevel ===3">店长</template>
          <el-button type="text" @click="changeLv(scope.row.userId,3)"
                     v-if="scope.row.vipLevel ===2 || scope.row.vipLevel ===4">设为店长
          </el-button>
          <br>
          <template v-if="scope.row.vipLevel ===4">boss</template>
          <el-button type="text" @click="changeLv(scope.row.userId,4)"
                     v-if="scope.row.vipLevel ===2 || scope.row.vipLevel ===3">设为boss
          </el-button>
        </template>
      </el-table-column>
      <el-table-column prop="agentId" label="邀请人id"></el-table-column>
      <el-table-column label="注册时间" prop="createTime"></el-table-column>
    </el-table>
    <div>
      <el-dialog title="用户资金记录" :modal-append-to-body="false" :visible.sync="show">
        <el-table :data="moneyData" border highlight-current-row stripe>
          <el-table-column label="类型" prop="remark"></el-table-column>
          <el-table-column label="数额" prop="money"></el-table-column>
          <el-table-column label="时间" prop="changeTime"></el-table-column>
        </el-table>
        <el-pagination
          background
          @current-change="jumpMoney"
          layout="total, prev, pager, next, jumper"
          :pageSize=15
          :total="moneyTotal">
        </el-pagination>
      </el-dialog>
    </div>
    <el-pagination
      background
      @current-change="jump"
      layout="total, prev, pager, next, jumper"
      :pageSize=15
      :total="total">
    </el-pagination>
    <el-dialog type="text" :modal-append-to-body="false" :visible.sync="bonusShow" width="30%">
      <el-form>
        <el-input v-model="bonus" type="number" placeholder="请输入金额"></el-input>
      </el-form>
      <span slot="footer" class="dialog-footer">
         <el-button @click="cancelBonus">取 消</el-button>
         <el-button type="primary" @click="doBonus">确 定</el-button>
       </span>
    </el-dialog>
    <el-dialog type="text" :modal-append-to-body="false" :visible.sync="showVoucher" width="30%">
      <el-form>
        <el-form-item label="用户id">
          <el-input v-model="voucher.userId" type="number" placeholder="用户id"></el-input>
        </el-form-item>
        <el-form-item label="抵用券金额">
          <el-input v-model="voucher.voucher" type="number" placeholder="请输入金额"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            placeholder="请选择用户等级"
            style="width: 150px"
            v-model="voucher.lv"
            clearable>
            <el-option v-for="item in level" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
         <el-button @click="showVoucher = false">取 消</el-button>
         <el-button type="primary" @click="submitVoucher">确 定</el-button>
       </span>
    </el-dialog>

  </div>
</template>

<script>
  import {ajax, mallUserList, moneyRecord, changeUserLevel, rewardUser, addUserVoucher} from "../../data/service";

  export default {
    name: "userList",
    data() {
      return {
        userData: [],
        page: 1,
        total: 0,
        wxNick: '',
        userId: '',
        telephone: '',
        nick: '',
        sortType: '',
        sortOrder: '',
        show: false,
        moneyData: [],
        moneyPage: 1,
        moneyTotal: 0,
        whoMoney: '',//查询余额的用户id,
        level: [
          {
            label: "全部",
            value: 0
          },
          {
            label: "普通顾客",
            value: 1
          },
          {
            label: "店员",
            value: 2
          },
          {
            label: "店长",
            value: 3
          },
          {
            label: "boss",
            value: 4
          },
        ],
        userType: 2,
        types: [
          {
            label: "全部",
            value: 0
          },
          {
            label: "小程序",
            value: 1
          },
          {
            label: "app",
            value: 2
          },
          {
            label: "公众号",
            value: 3
          }],
        bonus: 0,
        rewardTarget: '',//奖励的人
        bonusShow: false,
        vipLevel: '',
        agentId: '',
        showVoucher: false,
        voucher: {}//抵用券相关参数
      }
    },
    mixins: [ajax],
    created() {
      this.loadUser();
    },
    methods: {
      sortChange(option) {
        this.sortOrder = option.order ? (option.order === 'ascending' ? 'asc' : 'desc') : '';
        this.sortType = option.order ? option.prop : '';
        this.page = 1;
        this.loadUser();
      },

      loadUser() {
        this.doajax(mallUserList,
          {
            page: this.page,
            wxNick: this.wxNick === undefined ? "" : this.wxNick,
            userId: this.userId === undefined ? "" : this.userId,
            sortType: this.sortType,
            sortOrder: this.sortOrder,
            telephone: this.telephone === undefined ? "" : this.telephone,
            nick: this.nick === undefined ? "" : this.nick,
            userType: this.userType === 0 ? "" : this.userType,
            vipLevel: this.vipLevel === 0 ? "" : this.vipLevel,
            agentId: this.agentId === undefined ? "" : this.agentId
          },
          resp => {
            this.userData = resp.data;
            this.total = resp.total;
          }
        );
      },
      jump(val) {
        this.page = val;
        this.loadUser();
      },
      loadMoney() {
        this.doajax(moneyRecord,
          {
            userId: this.whoMoney,
            page: this.moneyPage,
          },
          resp => {
            this.moneyData = resp.data;
            this.moneyTotal = resp.total;
          });
      },
      jumpMoney(val) {
        this.moneyPage = val;
        this.loadMoney();
      },
      open(id) {
        this.whoMoney = id;
        this.moneyPage = 1;
        this.loadMoney();
        this.show = true;
      },
      changeLv(userId, lv = 2) {
        this.doajax(changeUserLevel, {userId: userId, level: lv}, resp => {
          this.success("修改成功");
          this.loadUser();
        })
      },
      openReward(userId) {
        this.rewardTarget = userId;
        this.bonusShow = true;
      },
      cancelBonus() {
        this.bonus = 0;
        this.bonusShow = false;
      },
      doBonus() {
        this.doajax(rewardUser, {userId: this.rewardTarget, money: this.bonus}, resp => {
          this.success("操作成功");
          this.rewardTarget = '';
          this.bonus = 0;
          this.bonusShow = false;
          this.loadUser();
        });
      },

      initVoucher() {
        this.voucher = {
          userId: '',
          voucher: 0,
          lv:2
        }
      },
      openVoucher() {
        this.showVoucher = true;
        this.initVoucher();
      },
      submitVoucher() {
        if (!this.voucher.userId) {
          this.error("请输入用户id");
          return;
        }
        if (this.voucher.voucher <= 0) {
          this.error("金额必须大于0");
          return;
        }
        this.doajax(changeUserLevel, {userId: this.voucher.userId, level: this.voucher.lv}, resp => {
          this.doajax(addUserVoucher,
            {
              userId:this.voucher.userId,
              voucher:this.voucher.voucher
            }, res => {
              this.success("修改成功");
              this.showVoucher = false;
              this.loadUser();
            });
          this.loadUser();
        });


      }
    }
  }
</script>

<style>

  .el-select .el-input__inner {
    width: 150px;
    -webkit-appearance: none;
    background-color: #fff;
    background-image: none;
    border-radius: 4px;
    border: 1px solid #dcdfe6;
    box-sizing: border-box;
    color: #606266;
    display: inline-block;
    font-size: inherit;
    height: 40px;
    line-height: 40px;
    outline: 0;
    padding: 0 15px;
    transition: border-color .2s cubic-bezier(.645, .045, .355, 1);
  }
</style>
